

package educar.abmMateria;


import educar.controllers.PrincipalController;
import educar.db.JPA;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Marcela Bessone.
 */

public class MateriaController implements ActionListener,MouseListener, WindowListener {
    
    private PrincipalController principalController;
    private MateriaView materiaView;
    
    
 // <editor-fold defaultstate="collapsed" desc="Constructor">
    public MateriaController(PrincipalController principalControl){ 
        principalController = principalControl;
        materiaView = new MateriaView();
        materiaView.setActionListener(this);
        materiaView.setMouseListener(this);
        materiaView.setWindowListener(this);
    }
 // </editor-fold>

      
    
 // <editor-fold defaultstate="collapsed" desc="Action setView">    
    public void setView() {
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int x = 0;
        int y = 50;
        materiaView.setBounds(x, y, screen.width, (screen.height)-90);
        materiaView.setModeloTabla(ListarMateria());
        materiaView.setVisible(true);
    }
// </editor-fold>  
    
// <editor-fold defaultstate="collapsed" desc="Metodos ActionPermormed">    
    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if (materiaView.getJbAgregar() == source) {
                AgregarMateriaController agregarMateriaController = new AgregarMateriaController(this);
                agregarMateriaController.setView();
        }
        
        if (materiaView.getJbEliminar() == source) {
            EliminarMateriaController eliminarMateriaController = new EliminarMateriaController();
            eliminarMateriaController.eliminarRegistro(materiaView.getModeloTabla());
        }    
        
        if (materiaView.getJbBuscar() == source) {
            materiaView.setModeloTabla(null);
            if (materiaView.getCriterio().isEmpty()){
                materiaView.setModeloTabla(ListarMateria());
            }else{  
                materiaView.setModeloTabla(buscarMateria(materiaView.getCriterio()));
            }
        }       
    }
    
    @Override
    public void mouseClicked(MouseEvent e) {
        Object source = e.getSource();
        if ((materiaView.getJTMateria() == source) && (e.getClickCount()==2)) {
               ModificarMateriaController modificarMateriaController = new ModificarMateriaController(this);
               int fila = materiaView.getJTMateria().getSelectedRow();
               Vector registro = (Vector) (materiaView.getModeloTabla().getDataVector()).elementAt(fila);
               modificarMateriaController.setView(registro, fila);
        }    
    }
// </editor-fold>

     
    /**
     * Lista todas las materias que dicta un docente responsable.
     * @param materiaCriterio: es el dni del docente
     * @return 
     */
    public DefaultTableModel buscarMateria(String materiaCriterio) {
            JPA jpa = new JPA();
            Object[] vector = new Object[4];
            DefaultTableModel tabla = (DefaultTableModel) materiaView.getModeloTabla(); 
            ResultSet res = jpa.getByField("materia", "DNI_responsable", materiaCriterio);
            try {
                while (res.next()) { //Existe siguiente registro.
                    vector[0] = false;
                    vector[1] = res.getObject(1);
                    vector[2] = res.getObject(2);
                    vector[3] = res.getObject(3); 
                    tabla.addRow(vector);
                }
            } catch (SQLException ex) { //No existe siguiente registro.
                ex.printStackTrace();
            }
            return tabla;       
    }
    
    /**
     * 
     * @return 
     */
    public DefaultTableModel ListarMateria() {
            JPA jpa = new JPA();
            Object[] vector = new Object[4];
            DefaultTableModel tabla = (DefaultTableModel) materiaView.getJTMateria().getModel(); 
            ResultSet res = jpa.showAll("materia");
            try {
                while (res.next()) {
                    vector[0] = false;
                    vector[1] = res.getObject(1);
                    vector[2] = res.getObject(2);
                    vector[3] = res.getObject(3); 
                    tabla.addRow(vector);
                }
            } catch (SQLException ex) {
            ex.printStackTrace();
            }
            return tabla;
    }
    
    
// <editor-fold defaultstate="collapsed" desc="Metodos auxiliares usados">    
    public void agregarATabla(String[] item){
        Object[] vector = new Object[4];
        vector[0] = (boolean) false;
        for (int i = 1; i < 4; i++){
            vector[i] = item[i-1];
        }
        materiaView.getModeloTabla().addRow(vector);
    }

    public void actualizarFila(String[] item, int fila){
        materiaView.getModeloTabla().setValueAt(false, fila, 0);
        for (int i = 1; i < 3; i++){
            materiaView.getModeloTabla().setValueAt(item[i-1], fila, i);
        }
    }
// </editor-fold> 
       
// <editor-fold defaultstate="collapsed" desc="Metodos ActionPerformed no usadas"> 
    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

    @Override
    public void windowOpened(WindowEvent e) {

    }

    @Override
    public void windowClosed(WindowEvent e) {

    }

    @Override
    public void windowIconified(WindowEvent e) {

    }

    @Override
    public void windowDeiconified(WindowEvent e) {

    }

    @Override
    public void windowActivated(WindowEvent e) {

    }

    @Override
    public void windowDeactivated(WindowEvent e) {

    }
  

    @Override
    public void windowClosing(WindowEvent e) {
    }
// </editor-fold>    
    
}  //fin clase MateriaController.